package com.compass.mobile.ui.fragment

import UserInfoManager
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.BaseGridView
import androidx.leanback.widget.FocusHighlight
import androidx.leanback.widget.FocusHighlightHelper
import androidx.lifecycle.lifecycleScope
import com.compass.mobile.callback.OnItemClickListener
import com.compass.mobile.databinding.FragmentCaseItemBinding
import com.compass.mobile.framework.base.BaseVBFragment
import com.compass.mobile.framework.constant.INSPECT_ORDER
import com.compass.mobile.framework.constant.TEST_ORDER_ACTION
import com.compass.mobile.framework.constant.YD_USER_ID
import com.compass.mobile.framework.ext.gone
import com.compass.mobile.framework.ext.onClick
import com.compass.mobile.framework.ext.visible
import com.compass.mobile.framework.utils.FocusViewUtils
import com.compass.mobile.framework.utils.LiveDataBus
import com.compass.mobile.framework.utils.dpToPx
import com.compass.mobile.models.Inspection
import com.compass.mobile.presenters.MyInspectionPresenter
import com.compass.mobile.ui.activity.InspectionOrderDetailsActivity
import com.compass.mobile.ui.dialog.CodeDialog
import com.compass.mobile.ui.dialog.CustomerServiceCode
import com.compass.mobile.ui.viewModel.InquiryViewModel
import com.compass.mobile.widget.MyBridgeAdapter
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * Description： 检验检查fragment
 * Date:2024/6/20 21:44
 */
class MyInspectionFragment : BaseVBFragment<FragmentCaseItemBinding>() {
    private val inquiryViewModel: InquiryViewModel by viewModels()
    private lateinit var arrayObjectAdapter: ArrayObjectAdapter
    private var inspections: MutableList<Inspection> = mutableListOf()
    private var mPage = 1
    private var isHaveNext = true
    private var type: String = "1"//传啥都一样
    override fun initView(view: View, savedInstanceState: Bundle?) {
        specialColumn()

        FocusViewUtils.scaleView(mBinding?.tvPrevious, 1)
        FocusViewUtils.scaleView(mBinding?.tvNextPage, 1)

    }


    override fun initData() {
        inquiryViewModel.getInspectionList(UserInfoManager.getPatientInfo()?.id, type, mPage)
        inquiryViewModel.inspectionLiveData.observe(this) {
            dismissLoading()
            if (mPage == 1) {
                if (it.isNullOrEmpty()) {
                    isHaveNext(0)
                    arrayObjectAdapter.clear()
                    mBinding?.emptyView?.visible()
                    mBinding?.llBelow?.gone()
                } else {
                    isHaveNext(it.size)
                    mBinding?.emptyView?.gone()
                    inspections = it
                    arrayObjectAdapter.clear()
                    arrayObjectAdapter.addAll(0, inspections)
                }
            } else {
                if (it.isNullOrEmpty()) {
                    isHaveNext(0)
                    mPage-- //恢复上一次的page
                    showToast("没有下一页了")
                } else {
                    isHaveNext(it.size)
                    inspections = it
                    arrayObjectAdapter.clear()
                    arrayObjectAdapter.addAll(0, inspections)
                }
            }
        }
        inquiryViewModel.consultationOrderLiveData.observe(this) {
            dismissLoading()
            mPage = 1
            inquiryViewModel.getInspectionList(UserInfoManager.getPatientInfo()?.id, type, mPage)
        }

        LiveDataBus.get().with(TEST_ORDER_ACTION, String::class.java)
            .observe(this) {
                showToast("支付成功")
                inquiryViewModel.getInspectionList(UserInfoManager.getPatientInfo()?.id, type, mPage)
                CustomerServiceCode.Builder(requireActivity(), TEST_ORDER_ACTION).show()
            }
    }

    private fun isHaveNext(number: Int) {
        if (number < 10) {
            isHaveNext = false
        } else {
            mBinding?.llBelow?.visible()
            isHaveNext = true
        }
    }


    override fun intListener() {
        mBinding?.tvPrevious?.onClick {
            if (mPage > 1) {
                mPage--
                inquiryViewModel.getInspectionList(
                    UserInfoManager.getPatientInfo()?.id,
                    type,
                    mPage
                )
            } else {
                showToast("没有上一页了")
            }
        }

        mBinding?.tvNextPage?.onClick {
            if (isHaveNext) {
                mPage++
                inquiryViewModel.getInspectionList(
                    UserInfoManager.getPatientInfo()?.id,
                    type,
                    mPage
                )
            } else {
                showToast("没有下一页了")
            }
        }
    }


    private val onItemClickListener: OnItemClickListener = object : OnItemClickListener {
        override fun onItemClick(position: Int, view: View) {
            val item = inspections[position]
            InspectionOrderDetailsActivity.start(requireContext(),item.id)


        }
    }


    @SuppressLint("RestrictedApi")
    fun specialColumn() {
        mBinding?.caseItem?.setNumColumns(2)
        mBinding?.caseItem?.horizontalSpacing = dpToPx(10)
        mBinding?.caseItem?.verticalSpacing = dpToPx(10)

        arrayObjectAdapter = ArrayObjectAdapter(MyInspectionPresenter())
        val itemBridgeAdapter = MyBridgeAdapter(
            requireContext(),
            arrayObjectAdapter, onItemClickListener
        )

        mBinding?.caseItem?.setAdapter(itemBridgeAdapter)
        mBinding?.caseItem?.setFocusScrollStrategy(BaseGridView.FOCUS_SCROLL_ITEM)
        arrayObjectAdapter.addAll(0, inspections)
        FocusHighlightHelper.setupBrowseItemFocusHighlight(
            itemBridgeAdapter,
            FocusHighlight.ZOOM_FACTOR_NONE, false
        )
        lifecycleScope.launch {
            delay(500)
            //延迟获取焦点
            mBinding?.caseItem?.requestFocus()
        }
    }
}